package com.yunxi.service.localService.impl.otd;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunxi.core.service.ServiceImplPlus;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.dao.otd.OtdOrderMonthVersionMapper;
import com.yunxi.model.otd.OtdOrderMonth;
import com.yunxi.model.otd.OtdOrderMonthVersion;
import com.yunxi.service.localService.otd.OtdOrderMonthService;
import com.yunxi.service.localService.otd.OtdOrderMonthVersionService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * <p>
 * 月排产需求订单版本管理 服务实现类
 * </p>
 *
 * @author XUQI
 * @since 2019-07-15
 */
@Service
public class OtdOrderMonthVersionServiceImpl extends
    ServiceImplPlus<OtdOrderMonthVersionMapper, OtdOrderMonthVersion> implements
    OtdOrderMonthVersionService {

    @Autowired
    @Qualifier("otdOrderMonthServiceImpl")
    OtdOrderMonthService otdOrderMonthService;

    /**
     * 删除月计划版本
     *
     * @param ids IDS
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ApiResult removeByIds(List<String> ids) {
        // 删除月计划
        LambdaQueryWrapper<OtdOrderMonth> monthWrapper = new LambdaQueryWrapper<>();
        monthWrapper.in(OtdOrderMonth::getVersionId, ids);
        otdOrderMonthService.remove(monthWrapper);
        // 删除版本记录
        LambdaQueryWrapper<OtdOrderMonthVersion> versionWrapper = new LambdaQueryWrapper<>();
        versionWrapper.in(OtdOrderMonthVersion::getId, ids);
        int delCount = baseMapper.delete(versionWrapper);
        if (delCount != ids.size()) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseUtil.fail(-1, "请稍后重试");
        }
        return ResponseUtil.ok();
    }
}
